package v1

import (
	"slices"
	"strconv"
	"yuanxin-admin/internal/common/ioc"
	"yuanxin-admin/internal/common/session"
	"yuanxin-admin/internal/dto"
	"yuanxin-admin/internal/repository"
	"yuanxin-admin/internal/service"

	"gitee.com/botaoshow/yuanxin/web/response"
	"github.com/gin-gonic/gin"
)

type SysMenuController struct {
	sysMenuService service.ISysMenuService
}

func NewSysMenuController() *SysMenuController {
	return &SysMenuController{
		sysMenuService: ioc.Inject[service.ISysMenuService]((service.ISysMenuService)(nil)),
	}
}

func (s *SysMenuController) ListAll(c *gin.Context) {
	if list, err := s.sysMenuService.SelectMenuList(c); err != nil {
		response.Fail(c, err.Error())
		return
	} else {
		response.Success(c, list)
	}
}
func (s *SysMenuController) RoleMenuAll(c *gin.Context) {
	if list, err := s.sysMenuService.RoleMenu(c); err != nil {
		response.Fail(c, err.Error())
		return
	} else {
		response.Success(c, list)
	}
}
func (s *SysMenuController) RoleMenu(c *gin.Context) {
	roles := session.GetRoleCodes(c)
	if slices.Contains(roles, "admin") {
		if list, err := s.sysMenuService.RoleMenu(c); err != nil {
			response.Fail(c, err.Error())
			return
		} else {
			response.Success(c, list)
			return
		}
	}

	if list, err := s.sysMenuService.RoleMenuByRoles(c, roles); err != nil {
		response.Fail(c, err.Error())
		return
	} else {
		response.Success(c, list)
	}
}
func (s *SysMenuController) GetRoleMenuIds(c *gin.Context) {
	res := make([]int64, 0)
	roleId := c.Param("roleId")
	err := repository.SysRoleMenu.WithContext(c).
		Select(repository.SysRoleMenu.MenuID).
		Where(repository.SysRoleMenu.RoleID.Eq(roleId)).
		Scan(&res)
	if err != nil {
		response.Fail(c, err.Error())
		return
	}
	response.Success(c, res)
}
func (s *SysMenuController) UpdateRoleMenus(c *gin.Context) {
	roleId := c.Param("roleId")

	var ids []int64
	if err := c.ShouldBind(&ids); err != nil {
		response.Fail(c, err.Error())
		return
	}
	if b, err := s.sysMenuService.UpdateRoleMenus(c, roleId, ids); err != nil {
		response.Fail(c, err.Error())
		return
	} else {
		response.ToAjax(c, b)
	}
}
func (s *SysMenuController) Create(c *gin.Context) {
	var d dto.SysMenuDTO
	if err := c.ShouldBind(&d); err != nil {
		response.Fail(c, err.Error())
		return
	}
	if d.Icon == nil {
		var icon = "Setting"
		d.Icon = &icon
	}
	entity := d.ToEntity()

	if b, err := s.sysMenuService.Create(c, entity); err != nil {
		response.Fail(c, err.Error())
		return
	} else {
		response.ToAjax(c, b)
	}
}
func (s *SysMenuController) Update(c *gin.Context) {
	id := c.Param("id")
	mid, _ := strconv.Atoi(id)

	var d dto.SysMenuDTO
	if err := c.ShouldBind(&d); err != nil {
		response.Fail(c, err.Error())
		return
	}
	entity := d.ToEntity()
	entity.MenuID = int64(mid)
	if b, err := s.sysMenuService.Update(c, entity); err != nil {
		response.Fail(c, err.Error())
		return
	} else {
		response.ToAjax(c, b)
	}

}
func (s *SysMenuController) QueryById(c *gin.Context) {
	id := c.Param("id")
	mid, _ := strconv.Atoi(id)
	sysMenu, err := repository.SysMenu.WithContext(c).Where(repository.SysMenu.MenuID.Eq(int64(mid))).First()
	if err != nil {
		response.Fail(c, err.Error())
		return
	}
	response.Success(c, sysMenu)
}

func (s *SysMenuController) DeleteById(c *gin.Context) {
	id := c.Param("id")
	mid, _ := strconv.Atoi(id)
	if b, err := s.sysMenuService.SafeDeleteById(c, int64(mid)); err != nil {
		response.Fail(c, err.Error())
		return
	} else {
		response.ToAjax(c, b)
	}
}
